// 路由数组扁平化
export const flatten = (arr: any) => {
    let result: any = [];
    for (let i = 0; i < arr.length; i++) {
        if (Array.isArray(arr[i].routes)) {
            result = result.concat(flatten(arr[i].routes));
        } else {
            result.push(arr[i]);
        }
    }
    return result;
};


const echartsDom: any = [];  //所有echarts图表的数组
/**
 * 当屏幕尺寸变化时，循环数组里的每一项调用resize方法来实现自适应。
 * @param {*} eDom
 */
export function echartsResize(eDom: any) {
    echartsDom.push(eDom);
    window.onresize = () => {
        echartsDom.forEach((it: any) => {
            it.resize();
        })
    };
}

export enum CloneType {
    Object = "Object",
    Array = "Array",
    Date = "Date",
    RegExp = "RegExp",
    Function = "Function",
    String = "String",
    Number = "Number",
    Boolean = "Boolean",
    Undefined = "Undefined",
    Null = "Null",
    Symbol = "Symbol",
    Set = "Set",
    Map = "Map"
}

export type _CloneType = keyof typeof CloneType

/**
 * 检测数据类型
 * @param type cloneType
 * @param obj 检测的数据源
 * @returns Boolean
 */
function isType<T>(type: _CloneType, obj: T) {
    return Object.prototype.toString.call(obj) === `[object ${type}]`;
}

/**
 * 深拷贝
 * @param obj 要克隆的对象
 * @param cache 缓存对象，用于解决循环引用的问题
 *  */
export function deepClone(data: any): any {
    // 获取传入拷贝函数的数据类型
    const type = typeOf(data)
    // 定义一个返回any类型的数据
    let reData: any;
    // 递归遍历一个array类型数据，
    if (type === "array") {
        reData = []
        for (let i = 0; i < data.length; i++) {
            reData.push(deepClone(data[i]))
        }
    } else if (type === "object") { //递归遍历一个object类型数据
        reData = {}
        for (const i in data) {
            reData[i] = deepClone(data[i])
        }
    }
    else {
        // 返回基本数据类型
        return data
    }
    // 将any类型的数据return出去，作为deepClone的结果
    return reData
}

function typeOf(obj: any): any {
    const toString: any = Object.prototype.toString
    const map: any = {
        "[object Boolean]": "boolean",
        "[object Number]": "number",
        "[object String]": "string",
        "[object Function]": "function",
        "[object Array]": "array",
        "[object Date]": "date",
        "[object RegExp]": "regExp",
        "[object Undefined]": "undefined",
        "[object Null]": "null",
        "[object Object]": "object"
    }
    return map[toString.call(obj)]
}
